package com.hpeu_01.day0126;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * 通过反射还原Person类
 */
public class ReflectPerson {
    public static void main(String[] args) {
        Class<Person> clazz = Person.class;
        //获取类名
        String classname = clazz.getSimpleName();
        //获取修饰类
        String s = Modifier.toString(clazz.getModifiers());
        //获取分类
        Class<? super Person> superclass = clazz.getSuperclass();
        System.out.println(s+" class "+classname+" extends "+superclass.getSimpleName()+" {");

        //获取字段
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            System.out.println("\t"+Modifier.toString(field.getModifiers())+" "+field.getType().getSimpleName()+" "+field.getName()+";");
        }

        //获取构造方法
        Constructor<?>[] constructors = clazz.getDeclaredConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.print("\t"+Modifier.toString(constructor.getModifiers())+" "+clazz.getSimpleName()+"(");
            //获取参数列表
            Class<?>[] parameterTypes = constructor.getParameterTypes();
            int index = 0;
            for (Class<?> parameterType : parameterTypes) {
                System.out.print(parameterType.getSimpleName()+" args"+index);
                if (index< parameterTypes.length-1){
                    System.out.print(",");
                }
                index++;
            }
            System.out.println(") {");
            System.out.println("\t}");
        }

        //获取方法
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            System.out.print("\t"+Modifier.toString(method.getModifiers())+" "+method.getReturnType().getSimpleName()+" "+method.getName()+"(");

            //获取参数列表
            Class<?>[] parameterTypes = method.getParameterTypes();
            int index = 0;
            for (Class<?> parameterType : parameterTypes) {
                System.out.print(parameterType.getSimpleName()+" arg"+index);
                if (index<parameterTypes.length-1){
                    System.out.print(",");
                }
            }
            System.out.println(") {");
            System.out.println("\t}");
        }
        System.out.println("}");
    }
}
